package com.jingyanku.struts2;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.jingyanku.pojo.Board;
import com.jingyanku.pojo.Category;
import com.jingyanku.pojo.Reply;
import com.jingyanku.pojo.Thread;
import com.jingyanku.service.IReplyService;
import com.jingyanku.service.IThreadService;
import com.opensymphony.xwork2.ActionSupport;
import com.system.pojo.Person;
import com.system.service.IPersonService;
import com.system.struts2.util.PersonInfo;
import com.system.struts2.util.PersonUtil;

public class ReplyAction extends ActionSupport {

	private Person person;

	private Board board;

	private Thread thread;

	private Reply reply;

	private Category category;

	private IPersonService<Person> personSer;

	private IThreadService<Thread> threadSer;

	private IReplyService<Reply> replySer;

	HttpServletRequest request = null;

	HttpServletResponse response = null;

	public String list() {

		return null;
	}

	public String initAdd() {

		thread = threadSer.find(Thread.class, thread.getId());
		board = thread.getBoard();
		category = thread.getBoard().getCategory();

		return "initAdd";// new ActionForward("add", "/form/reply/addReply.jsp",
							// false);
	}

	public String add() {
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
		thread = threadSer.find(Thread.class, thread.getId());
		PersonInfo personInfo = PersonUtil.getPersonInfo(request, response);
		Person person = personSer.find(Person.class, personInfo.getId());

		reply.setThread(thread);
		reply.setDateCreated(getSystemDate());
		reply.setDeleted(false);
		reply.setFloor(0);
		reply.setAuthor(person);
		reply.setIpCreated(request.getRemoteAddr());
		replySer.create(reply);

		board = thread.getBoard();
		category = thread.getBoard().getCategory();

		return "add";// new ActionForward("success", "/form/reply/success.jsp",
						// false);
	}

	public String delete() {
		reply = replySer.find(Reply.class, reply.getId());
		reply.setDeleted(true);
		reply.getThread().setReplyCount(reply.getThread().getReplyCount() - 1);
		replySer.save(reply);
		threadSer.save(reply.getThread());
		thread = reply.getThread();
		return "delete";
	}

	public Date getSystemDate() {

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String time = df.format(new Date());
		return Timestamp.valueOf(time);
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public Board getBoard() {
		return board;
	}

	public void setBoard(Board board) {
		this.board = board;
	}

	public Thread getThread() {
		return thread;
	}

	public void setThread(Thread thread) {
		this.thread = thread;
	}

	public Reply getReply() {
		return reply;
	}

	public void setReply(Reply reply) {
		this.reply = reply;
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public IPersonService<Person> getPersonSer() {
		return personSer;
	}

	public void setPersonSer(IPersonService<Person> personSer) {
		this.personSer = personSer;
	}

	public IThreadService<Thread> getThreadSer() {
		return threadSer;
	}

	public void setThreadSer(IThreadService<Thread> threadSer) {
		this.threadSer = threadSer;
	}

	public IReplyService<Reply> getReplySer() {
		return replySer;
	}

	public void setReplySer(IReplyService<Reply> replySer) {
		this.replySer = replySer;
	}

}
